package com.portal.back.web;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.List;

import net.sf.json.JSONArray;

import com.portal.back.service.ISysMenuService;
import com.portal.back.service.impl.SysMenuService;
import com.portal.back.web.DTO.TreeDto;
import com.portal.base.web.BaseAction;
import com.portal.domain.SysMenu;
import com.portal.util.common.CharacterUtil;
import com.portal.util.common.Contants;

/**
 * @Title:后台菜单Action
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2010-3-28
 * 
 */
@SuppressWarnings("serial")
public class SysMenuAction extends BaseAction {
	private ISysMenuService service;
	private String id;
	private String menuName;
	private String menuUrl;
	private String menuDescribe;

	public SysMenuAction() {
		service = new SysMenuService();
	}

	@SuppressWarnings("unchecked")
	public String getTree() {
		response.reset();
		response.setCharacterEncoding("utf-8");
		try {
			List<TreeDto> list = service.getByParentId(Integer.parseInt(id));
			// List<SysMenu> list = service. getAll();
			JSONArray jsonObject = JSONArray.fromObject(list);
			this.jsonString = jsonObject.toString();
		} catch (Exception e) {
			log.error("获取后台菜单错误", e);
			jsonString = "[]";
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	public String addMenu() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		String json = "{failure:true,msg:'菜单添加失败'}";
		boolean isFlag = service.addMenu(CharacterUtil.encode(menuName),
				menuUrl, CharacterUtil.encode(menuDescribe), id);
		if (isFlag)
			json = "{success:true, msg:'菜单添加成功'}";
		try {
			out = response.getWriter();
			out.println(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String deleteMenu() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		SysMenu menu = service.getById(id);
		if (menu == null) {
			message = "数据库已经没有菜单项了";

		} else {
			message = "菜单删除失败";
			boolean isFlag = service.deleteMenuByid(id);
			if (isFlag)
				message = "菜单删除成功";
		}
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public String queryMenu() {
		SysMenu menu = service.getById(id);
		request.setAttribute("menu", menu);
		return "querymenu";
	}

	public String editMenu() {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			boolean isFlag = service.editByid(CharacterUtil.encode(menuName),
					menuUrl, CharacterUtil.encode(menuDescribe), id);
			out = response.getWriter();
			if (isFlag)
				out
						.println("<script>alert('修改成功');top[1].location.reload();</script>");
			else
				out
						.println("<script>alert('修改失败'); top[1].location.reload();</script>");

		} catch (Exception e) {

			e.printStackTrace();
		}
		return null;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getMenuUrl() {
		return menuUrl;
	}

	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}

	public String getMenuDescribe() {
		return menuDescribe;
	}

	public void setMenuDescribe(String menuDescribe) {
		this.menuDescribe = menuDescribe;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
